<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head profile="http://gmpg.org/xfn/11">
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Narrative JavaScript: Compilation</title>
	<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
	<style>
	textarea { width:100%; height:200px; }
	</style>
</head>
<body>

<div id="page">
	<div id="header">
		<h1><a href="index.html">Fluid JavaScript</a></h1>
	</div>

	<div id="content" class="narrowcolumn" style="margin:0px; ">


		<div class="navigation">
			<div class="alignleft">&laquo; <a href="example.html">example</a></div>
			<div class="alignright"> <a href="reference.html">reference</a> &raquo;</div>
		</div>

		<div class="post">

			<h2>Compilation</h2>
			<p>
			Included in the Narrative JavaScript distribution is a shell script
			name njsc.  You can use this script to compile Narrative JavaScript
			source files:
			<pre>
    njsc source_file.njs
			</pre>
			This will create a compiled script file name source_file.js in the
			same directory as the .njs file.

			In order to use the script, you'll need a JavaScript shell, such
			as the one provided by the <a href="http://www.mozilla.org/rhino/">Rhino</a> project.
			</p>
			<p>
			Configure the compiler with the location of your shell by setting the
			JAVASCRIPT_SHELL environment variable.  For example:
			<pre>
    export JAVASCRIPT_SHELL="java -cp js.jar org.mozilla.javascript.tools.shell.Main"
			</pre>
			</p>

			<h2>Compiler Options</h2>
			<p>
			<i>exceptions=[on|off|true|false|1|0|yes|no]</i>
			</p>
			<p>
			Turns on or off support for exception handling within blocking
			methods.  The default is "on", which is appropriate for most
			environments.  Some environments don't support or have poor support
			for exception handling; in such cases it is advisable to turn
			this flag off.
			</p>

			<h2>Try it Out</h2>
			<p>
			Enter some Narrative JavaScript code here:<br>
			<textarea id="input"></textarea><br>
			<input id="exceptions" type=checkbox checked value="on"> exception support<br>
			<input id="hsql" type=checkbox > Do it for HSQL<br>
			<button onclick="compile()">Compile</button>
			</p>
			<p>
			Compiled code:<br>
			<textarea id="output"></textarea>
			</p>
			<script src="../editor/Narcissus.js"></script>
			<script src="../editor/StrandsCompiler.js"></script>
			<script src="../editor/StrandsScriptWriter.js"></script>
			<script src="../editor/compressor.js"></script>
			<script>
			function compile() {
				_frm = false;
				njs = false;
				var input = document.getElementById("input").value;
				var exceptions = document.getElementById("exceptions").checked ? "true" : "false";
				var options = {exceptions: exceptions, getters:true,setters:true,compress : false, debug: true, persistence: false};
				var compiler = new StrandsCompiler(options);
				try {
					if (document.getElementById("hsql").checked)
						input = decodeHSQL(input);
					var start = new Date().getTime();
					var output = compiler.compile(input, "input");
					if (document.getElementById("hsql").checked)
						output = compress(output);
					document.getElementById("output").value = output;
				} catch(e) {
					document.getElementById("output").value = "";
					do {
						document.getElementById("output").value += "\nERROR: line " + e.lineNumber + ": " + e.message;
					} while (e = e.next);				
				}
			}
			</script>

			<h2>Compiler API</h2>
			<p>
			Since the compiler is written in JavaScript, other modes of compilation
			are possible, such as dynamic loading of JavaScript from a web server.
			In order create your own compiler, you need to start by loading the
			following files into your JavaScript environment:
			<ul>
				<li>narcissus_jsdefs.js</li>
				<li>narcissus_jsparse.js</li>
				<li>StrandsCompiler.js</li>
				<li>StrandsScriptWriter.js</li>
			</ul>
			Then simply create and invoke a new compiler:
			<pre>
    var compiler = new StrandsCompiler();
    try {
    	// options is a hash table of name/value pairs corresponding to
    	// the command-line options for the compiler.
        var output_code = compiler.compile(input_code, filename, options)
    } catch(e) {
        // e is a JavaScript Error object
        // do error notification here
    }
			</pre>
			</p>
		</div>
	</div>

	<div id="footer">
	Narrative JavaScript was created by <a href="http://www.neilmix.com/">Neil Mix</a><br />
	<a href="http://www.azeemazeez.com/stuff/themes/" title="White as Milk theme for Wordpress">White as Milk</a> graphic design by <a href="http://www.azeemazeez.com">Azeem Azeez</a>.<br />
	</div>
</div>



</body>
</html>